package ServerTCP;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Arrays;

public class ThroughputTCP extends Thread {
    Socket s;
    double size;
    String mess;

    public ThroughputTCP(Socket socket) {
        this.s = socket;
        byte[] b = new byte[1024];
        Arrays.fill(b, (byte) 0);
        mess = new String(b);//让mess作为一个大小为1024B的串用来测吞吐量
        size = 0.0;
    }

    @Override
    public void run() {
        long time1=System.currentTimeMillis();
        long time2=time1;
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            System.out.println("开始发包！");

            while (time2-time1<1000) {//发包1秒
                bw.write(mess + "\n");
                bw.flush();
                size += 0.001;//每次传输1024B=1Kb=0.001Mb
                time2=System.currentTimeMillis();
            }
            System.out.println("结束发包！");
            bw.write("over\n");
            bw.flush();
            bw.write(size + "\n");//计算出吞吐量  Mbs
            bw.flush();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

